#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

#define N 1000000000
#define MY_RAND_MAX 2147483647

int old_gnu_rand_r (unsigned int *);

int main(int argc, char* argv[])
{
    int i, niter, count;
    unsigned int seed;
    double x , y, z, result;

    count = 0;
    seed = time(NULL);

    for (i = 0; i < N; i++)
    {
        x = (double)old_gnu_rand_r(&seed) / MY_RAND_MAX;
        y = (double)old_gnu_rand_r(&seed) / MY_RAND_MAX;
        z = (x * x) + (y * y);
        if (z <= 1) count++;
    }

    result = ((double)count / N) * 4;
    printf("%f \n", result);

    return 0;
}

/* Reentrant random function from POSIX.1c.
   Copyright (C) 1996, 1999, 2009 Free Software Foundation, Inc.
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. */
int old_gnu_rand_r (unsigned int *seed)
{
    unsigned int next = *seed;
    int result;

    next *= 1103515245;
    next += 12345;
    result = (unsigned int) (next / 65536) % 2048;

    next *= 1103515245;
    next += 12345;
    result <<= 10;
    result ^= (unsigned int) (next / 65536) % 1024;

    next *= 1103515245;
    next += 12345;
    result <<= 10;
    result ^= (unsigned int) (next / 65536) % 1024;

    *seed = next;

    return result;
}

